package com.tensquare.article.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 文章(TbArticle)表控制层
 *
 * @author long
 * @since 2021-07-14 11:51:08
 */
@CrossOrigin
@RestController
@RequestMapping("article")
public class ArticleController {
    /**
     * 服务对象
     */
    @Resource
    private ArticleService articleService;
    @Autowired
    private StringRedisTemplate strRedisTemplate;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable String id) {
        Article article = articleService.findById(id);
        return new Result(true, "查询成功", StatusCode.OK, article);
    }

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        List<Article> list = articleService.findAll();
        return new Result(true, "查询成功", StatusCode.OK, list);
    }

    /**
     * 新增文章数据接口
     * RequestBody只能识别Content-Type为application/json的参数，其他的会报错
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Article article) {
        articleService.add(article);
        return new Result(true, "添加成功", StatusCode.OK);
    }

    /**
     * 修改文章数据接口
     */
    @RequestMapping(method = RequestMethod.PUT, value = "{id}")
    public Result update(@RequestBody Article article, @PathVariable String id) {
        article.setId(id);
        articleService.update(article);
        return new Result(true, "修改成功", StatusCode.OK);
    }

    /**
     * 删除文章数据接口
     */
    @RequestMapping(method = RequestMethod.DELETE, value = "{id}")
    public Result delete(@PathVariable String id) {
        articleService.delete(id);
        return new Result(true, "删除成功", StatusCode.OK);
    }

    /**
     * 条件查询
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result search(@PathVariable int page, @PathVariable int size, @RequestBody Map<String, String> map) {
        Page<Article> articlePage = articleService.search(map, size, page);
        return new Result(true, "查询成功", StatusCode.OK, new PageResult<>(articlePage.getSize()
                , articlePage.getRecords()));
    }

    /**
     * 订阅或取消订阅文章作者
     */
    @RequestMapping(value = "/subscribe", method = RequestMethod.POST)
    public Result subscribe(HttpServletRequest request, String authorId) {
        System.out.println("authorId:"+authorId);
        boolean subscribe = articleService.subscribe(request.getHeader("userId"), authorId);
        if (subscribe) {
            return new Result(true, "订阅成功", StatusCode.OK);
        } else {
            return new Result(true, "订阅取消", StatusCode.OK);
        }
    }

    /**
     * 文章点赞
     *
     * @param userId
     * @param articleId
     * @return
     */
    @RequestMapping(value = "thumpup/{userId}/{articleId}", method = RequestMethod.PUT)
    public Result thumbUp(@PathVariable String userId,
                          @PathVariable String articleId) {
        String thumpUpKey = "thumpup_article_" + userId + "_" + articleId;
        String thumpUpValue = strRedisTemplate.opsForValue().get(thumpUpKey);
        if (null == thumpUpValue) {
            articleService.thumpUp(userId, articleId);
            strRedisTemplate.opsForValue().set(thumpUpKey, "1");
            return new Result(true, "点赞成功", StatusCode.OK);
        }
        return new Result(true, "不能重复点赞", StatusCode.REPERROR);
    }

}
